package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.soda.bean.Information;
import com.soda.bean.Notice;
import com.soda.dao.InformationDao;
import com.soda.dao.impl.InformationDaoImpl;
import com.soda.service.InformationService;
import com.soda.service.NoticeService;
import com.soda.service.impl.InformationServiceImpl;
import com.soda.service.impl.NoticeServiceImpl;
import com.soda.util.PageInfo;
import com.soda.util.SelectIndustryInfo;
import com.soda.util.SelectInformInfo;
import com.soda.util.SelectNoticeInfo;

public class InformationServlet extends HttpServlet {
	InformationService ins=new InformationServiceImpl();
	InformationDao ind=new InformationDaoImpl();
	SelectInformInfo sii = new SelectInformInfo();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String param = request.getParameter("param");// 获得参数，由参数判断执行方法
		if (param != null) {
			// 查看
			if ("Inform".equals(param)) {
 
				if(request.getParameter("inf")!=null){
					request.getSession().removeAttribute("sii");
				}
				Inform(request, response);
			}
			// 删除
			if ("delete".equals(param)) {

				try {
					delete(request, response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// 编辑内容
			if ("edit".equals(param)) {

				edit(request, response);
			}
			if ("update".equals(param)) {
				update(request, response);
			}
			// 增加
			if ("add".equals(param)) {
				add(request, response);
			}
			if("updatestate".equals(param)){
				updatestate(request,response);
			}
			// 根据ID全查
						if ("showAllInform".equals(param)) {
							showAllInform(request, response);
						}
			// 批量删除
			if ("alldelete".equals(param)) {
				alldelete(request, response);
			}
			//导出
			
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}
	

	// 增加
	protected void add(HttpServletRequest request, HttpServletResponse response) {
		String title = request.getParameter("Name");
		int recommend = Integer.parseInt(request.getParameter("recommend"));
		String neirong = request.getParameter("neirong");
		HttpSession session = request.getSession();

		Information inform = new Information();
		inform.setInfTitle(title);
		inform.setInfState(recommend);
		inform.setInfContent(neirong);
		int num = ins.addInform(inform);
		if (num > 0) {

			try {
				Inform(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 全查
	public void Inform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		 
		// 防止乱码

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
 
	
		//PageInfo pi = new PageInfo();
		PageInfo pi = new PageInfo(1, 5, 0, 0, "InformationServlet", "Inform");
		SelectInformInfo sii = new SelectInformInfo();
		// 从jsp中获取值
		String Title = request.getParameter("informTitle");
		String updateTime = request.getParameter("updateTime");
		String state = request.getParameter("select2");
		
		
		HttpSession session = request.getSession();
		if (session.getAttribute("sii") != null) {
			sii = (SelectInformInfo) session.getAttribute("sii");
		} else {
			sii = new SelectInformInfo("", "",-1);
		}

		if (Title != null) { // 分页条件赋值
			sii.setInfTitle(Title.length() == 0 ? "" : Title);

			sii.setInfUpdateTime(updateTime == null || updateTime.length() == 0 ? "":updateTime);
			sii.setInfState(state == null ||state.length() == 0  ? -1 : Integer.parseInt(state));
			request.getSession().setAttribute("sii", sii);
		} else {
			sii = (SelectInformInfo) request.getSession().getAttribute("sii");
		}

		int page = 1;
		pi.setPer(5);// 分页信息赋值
		int count=ins.getInformCount(sii);
		pi.setTotal(count);
		pi.setLastPage((count+pi.getPer()-1)/pi.getPer());
		
		String pages = request.getParameter("page");
 
		if (pages != null) {
			pi.setPage(Integer.parseInt(pages));
		}

		List<Information>list = ins.getInform(sii, pi);
	 
		request.setAttribute("Inform", list);
		request.setAttribute("page", pi);
		session.setAttribute("statess", state);
		request.getRequestDispatcher("houtai/pages/publicityManage/poineerMessage/index.jsp").forward(request, response);

	}

	// 单个删除
	protected void delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception, IOException {
		response.setContentType("text/html;charset=utf-8");
		int Id = Integer.parseInt(request.getParameter("id"));

		if (ins.deleteById(Id) > 0) {
			Inform(request, response);
		}

	}

	// 批量删除
	protected void alldelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String dellist = request.getParameter("id");
		 
		String []list=dellist.split(",");
 
		
		int num = ins.delAllInform(list);
		PrintWriter out=response.getWriter();
		if (num > 0) {
			Inform(request, response);
	 
			}
	}

	// 根据ID查信息
	protected void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		
		int id = Integer.parseInt(request.getParameter("editId"));
		Information inform = ins.showAllInform(id);
		request.setAttribute("Inform", inform);
		request.setAttribute("state", inform.getInfState());
		request.getRequestDispatcher("houtai/pages/publicityManage/poineerMessage/edit.jsp").forward(request, response);

	}
	protected void showAllInform(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		int id = Integer.parseInt(request.getParameter("Id"));
		Information inform=ins.showAllInform(id);
		request.setAttribute("Inform", inform);
		request.setAttribute("state", inform.getInfState());
		request.getRequestDispatcher("houtai/pages/publicityManage/poineerMessage/lookImpl.jsp").forward(request, response);

	
	
	}
	
	
	//修改状态
		public void updatestate(HttpServletRequest request,
				HttpServletResponse response) {
			// TODO Auto-generated method stub
			
			try {
				int state=Integer.parseInt(request.getParameter("state"));
				int newId=Integer.parseInt(request.getParameter("newid"));
				//更新
				int n=ins.updateStateNews(newId,state);
				PrintWriter out= response.getWriter();
				if(n>0){
					out.print("修改成功");
					//out.print("<script>location.href='NewsServlet?method='+'sel'</script>");
				}
				out.close();
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	
	
	
	
	// 修改信息
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int id = Integer.parseInt(request.getParameter("id"));
		int recommend = Integer.parseInt(request.getParameter("recommend"));
		String neirong = request.getParameter("neirong");

		int n = ins.updateInform(id, recommend, neirong);

		if (n > 0) {
			response.sendRedirect("InformationServlet?param=Inform");

		} else {
			System.out.println("shibai");
		}
	}}